﻿import com.dmotiko.general.*
import com.dmotiko.util.*
import com.dmotiko.chabeli.*
import mx.utils.Delegate;

class ChabeliSite
extends WebSite {
	
	public static var HOME:String = "chabeli_home";
	public static var PRODUCTOS:String = "chabeli_products";
	public static var NOSOTROS:String = "chabeli_nosotros";
	public static var CONTACTO:String = "chabeli_contacto";
	
	private static var app:ChabeliSite;
	
	private var productsLoader:DataXMLLoader;
	private var productsData:Object;
	
	public static function init(timeline:MovieClip){
		if (!app) {
			app = new ChabeliSite(timeline);
			app.initSite();
		} 
	}
	public static function getApp():ChabeliSite {
		return ChabeliSite(app);
	}
	
	private function ChabeliSite(timeline){
		super(timeline);
	}
		
	private function initSite(){
		setSection( ChabeliSite.CONTACTO );
		initStage();
		loadExternalData();
		//initSections();
	}
	
	private function loadExternalData():Void {
		productsData = new Object();
		productsLoader = new DataXMLLoader("products1.xml");		
		productsLoader.addEventListener( DataXMLLoader.DATA_LOADED, Delegate.create(this, productsLoaded) );
		
	}
	
	private function productsLoaded( e ):Void {
		if ( e.src == "products1.xml") {
			productsData.products1 = e.data;
			productsLoader.run( "products2.xml" );
		} else if ( e.src == "products2.xml") {
			productsData.products2 = e.data;
			productsLoader.run( "products3.xml" );
		} else if ( e.src == "products3.xml") { 
			productsData.products3 = e.data;
			productsLoader.run( "products4.xml" );
		} else if ( e.src == "products4.xml") {
			productsData.products4 = e.data;
			collectProducts();	
			initSections();
		}
		
	}
	
	/**
	 * @author: sminutoli
	 * @usage: genera una prop total con el random de todos los productos
	 */
	private function collectProducts():Void {
		var total:Array = new Array();
		for (var i:Number = 0; i < productsData.products1.product.length; i++ ) {
			total.push(productsData.products1.product[i]);
		}
		for (var i:Number = 0; i < productsData.products2.product.length; i++ ) {
			total.push(productsData.products2.product[i]);
		}
		for (var i:Number = 0; i < productsData.products3.product.length; i++ ) {
			total.push(productsData.products3.product[i]);
		}
		for (var i:Number = 0; i < productsData.products4.product.length; i++ ) {
			total.push(productsData.products4.product[i]);
		}
		productsData.total = ArrayUtil.randomArray(total);
	}
	
	private function initStage():Void {
		Stage.align = "TL";
		Stage.scaleMode = "noScale";
	
	}
	
	private function initSections() {
		var logo:ChabeliLogo = ChabeliLogo( timeline.attachMovie("chabeliLogo", "mcLogo", 4 ) );
		logo.addEventListener( BaseClipEvent.CHANGED, Delegate.create( this, logoShowed ) );
		timeline.attachMovie("footerComponent", "mcFooter", 2);
		
	}
	
	private function logoShowed() {
		//timeline.attachMovie("mainPhoto", "mcMainPhoto", 1);
		timeline.attachMovie("navBar", "mcNavBar", 8);
		timeline.attachMovie("sectionNosotros", "mcNosotros", 5, { _y: 280 } );
		timeline.attachMovie("sectionContacto", "mcContacto", 6, { _y: 280 } );
		timeline.attachMovie("sectionProducts", "mcProducts", 3 /*, { _y: 0, _x: 240 }*/);
	}
	
	public function setSection(section:String) {
		super.setSection(section);
		if (section == ChabeliSite.HOME) {
			timeline.attachMovie("mainPhoto", "mcMainPhoto", 1);
		}
	}
	
	public function getProductsData():Object { 
		return productsData; 
	}
}